
var axios = require('axios');
var fs = require('fs');
var result = { 'lines': {} }
fs.readFile('cad.json', function (err, data) {
    if (err) {
        return console.error(err);
    }
    var dxf = data.toString();//将二进制的数据转换为字符串
    dxf = JSON.parse(dxf);//将字符串转换为json对象
    var lines = {}
    dxf.map(function (e) {
        var line = []
        if (typeof lines[e.layer] == 'undefined') {
            lines[e.layer] = []
        }
        for (var i = 0; i < e.vertices.length; i += 3) {
            line.push({ x: e.vertices[i], y: e.vertices[i + 1] })
        }
        lines[e.layer].push(line)
    })
    for (var layer in lines) {
        var line = lines[layer], coords = []
        if (layer != 'DN80') {
            line.map(function (l, i) {
                if (l.length > 2) {
                    console.log(l.length)
                }
                coords = coords.concat(l.map(function (p) {
                    return getPoints(p)
                }))
            })
            for (var i = 0; i < coords.length; i += 100) {
                getCoords(coords.slice(i, i + 100).join(';'), { from: 1, to: 5 }, layer)
            }
        }

    }

    async function getCoords(points, params, layer) {
        await axios({
            method: 'post',
            url: 'http://api.map.baidu.com/geoconv/v1/',
            params: {
                coords: points,
                from: params.from,
                to: params.to,
                ak: 'gsgw6ce8LzsD9oRRHfKgpqCshjNyEGaL'

            }
        }).then((res) => {
            if (typeof result.lines[layer] == 'undefined') {
                result.lines[layer] = []
            }

            for (var i = 0; i < res.data.result.length; i += 2) {
                result.lines[layer].push({ 'coords': [[res.data.result[i].x, res.data.result[i].y], [res.data.result[i + 1].x, res.data.result[i + 1].y]] })
            }
            fs.writeFileSync('line.json', JSON.stringify(result));

        })
    }


})

var coords = '114.21892734521,29.575429778924;114.21892734521,29.575429778924'
var coords = '112.5376205444444445,37.43095'
//37°25′46.07″ 东经E112°32′15.41″
//37°25′45.52″ 东经E112°32′15.44″
console.log(112 + 33 / 60 + 10.81 / 3600, 37 + 24 / 60 + 57.13 / 3600)
//370420.4853765796,4145557.6137681473  112.53750555555555 37.4329
//372679.633696098,4144353.1520703947  112.56303055555556 37.422380555555556
function getPoints(p) {
    var p1 = { x: 370420.4853765796, y: 4145557.6137681473, e: 112.53750555555555, n: 37.4329 },
        p2 = { x: 371787.5737543399, y: 4143631.1452524215, e: 112.55300277777778, n: 37.415669444444446 }
    //371787.5737543399, y: 4143631.1452524215
    //37°24′57.13″ 东经E112°33′10.81
    //112.55300277777778 37.415869444444446
    var rx, ry, x, y
    rx = (p1.x - p2.x) / (p1.e - p2.e)
    ry = (p1.y - p2.y) / (p1.n - p2.n)
    x = p1.e + (p.x - p1.x) / rx
    y = p1.n + (p.y - p1.y) / ry
    return x + ',' + y
    //x*Math.cos(ang) - y*Math.sin(ang)
}
//372429.16270177666, y: 4145215.6051916587
//getCoords(getPoints({ x: 372416.2291437455, y: 4144782.165199714 }), { from: 1, to: 5 })